package com.fmatos.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fmatos.controller.directory.AlbumDirectory;
import com.fmatos.service.album.domain.Album;
import com.fmatos.service.album.downloader.info.AlbumInfo;
import com.fmatos.service.album.downloader.rss.AbstractRssDownloader;
import com.fmatos.service.album.downloader.rss.CustomRssDownloader;
import com.fmatos.service.album.downloader.rss.RssAlbumInfo;

@ComponentScan("com.fmatos.controller")

@Controller
@EnableAutoConfiguration
public class AlbumsController {

	private static final Logger LOGGER = LoggerFactory.getLogger(AlbumsController.class);
	private int requestCount = 0;
	
	private AlbumsCache albumsCache;
	
	public AlbumsController() {
	
		albumsCache = new AlbumsCache();
	}
	
    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }
	
	@RequestMapping(value="album/customrss", produces={"application/json"})
	public 
	@ResponseBody Album 
	parseRss(@RequestParam(value="url") String rssUrl)  {
		
		LOGGER.info("Parsing custom rss feed {}" , rssUrl);
		AlbumInfo albumInfo = new RssAlbumInfo("Rss feed " + rssUrl.hashCode(), rssUrl);
		Album album = albumsCache.getAlbum(albumInfo);
 
		requestCount++;
		return album;
	}

	@RequestMapping(value="count", produces={"application/json"})
	public 
	@ResponseBody int  
	getRequestCount()  {
		
		return requestCount++;
	}
	
	@RequestMapping(value="album", produces={"application/json"})
	public 
	@ResponseBody List<AlbumInfo> 
	getAlbumsList()  {
		
		return AlbumDirectory.getAllAlbums();
	}

    public static void main(String[] args) throws Exception {
        SpringApplication.run(AlbumsController.class, args);
    }
}
